package com.lesama.listener;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class KeyListenerTest {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLayout(null);
        frame.setTitle("键盘监听事件");
        frame.setSize(800, 500);
        frame.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
//                System.out.println("键盘按下了" + e.getKeyCode());
            }

            @Override
            public void keyPressed(KeyEvent e) {
//                System.out.println("键盘按下Pressed了" + e.getKeyCode());
                int keyCode = e.getKeyCode();
                if (keyCode == 37) {
                    System.out.println("左移动逻辑执行");
                } else if (keyCode == 38) {
                    System.out.println("上移动逻辑执行");
                } else if (keyCode == 39) {
                    System.out.println("右移动逻辑执行");
                } else if (keyCode == 40) {
                    System.out.println("下移动逻辑执行");
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
//                System.out.println("键盘松开了" + e.getKeyCode());
            }
        });
        frame.setVisible(true);
    }
}
